package com.example.javaproject.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.javaproject.entity.Administrator;
import com.example.javaproject.entity.User;
import com.example.javaproject.entity.Teacher;
import com.example.javaproject.entity.Student;
import com.example.javaproject.mapper.UserMapper;
import com.example.javaproject.mapper.TeacherMapper;
import com.example.javaproject.mapper.StudentMapper;
import com.example.javaproject.mapper.AdministratorMapper;
import com.example.javaproject.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    private TeacherMapper teacherMapper;
    
    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private AdministratorMapper administratorMapper;

    @Override
    public User login(String username, String password) {
        QueryWrapper<User> query = new QueryWrapper<>();
        query.eq("username", username)
             .eq("password", password)
             .eq("is_deleted", 0);
        return this.getOne(query);
    }

    @Override
    @Transactional
    public boolean updateUserRole(User user) {
        // 获取原始用户信息
        User originalUser = this.getById(user.getId());
        if (originalUser == null) {
            return false;
        }

        // 如果角色没有改变，直接更新用户信息
        if (originalUser.getRole().equals(user.getRole())) {
            return this.updateById(user);
        }

        // 处理原角色表中的数据
        if (originalUser.getRole() == 2) { // 原来是教师
            Teacher teacher = teacherMapper.selectOne(
                new QueryWrapper<Teacher>()
                    .eq("user_id", user.getId())
                    .eq("is_deleted", 0)
            );
            if (teacher != null) {
                teacher.setIsDeleted(1);
                teacher.setUpdateTime(LocalDateTime.now());
                teacherMapper.updateById(teacher);
            }
        } else if (originalUser.getRole() == 1) { // 原来是学生
            Student student = studentMapper.selectOne(
                new QueryWrapper<Student>()
                    .eq("user_id", user.getId())
                    .eq("is_deleted", 0)
            );
            if (student != null) {
                student.setIsDeleted(1);
                student.setUpdateTime(LocalDateTime.now());
                studentMapper.updateById(student);
            }
        } else if (originalUser.getRole() == 0) { // 原来是管理员
            Administrator admin = administratorMapper.selectOne(
                new QueryWrapper<Administrator>()
                    .eq("user_id", user.getId())
                    .eq("is_deleted", 0)
            );
            if (admin != null) {
                admin.setIsDeleted(1);
                admin.setUpdateTime(LocalDateTime.now());
                administratorMapper.updateById(admin);
            }
        }

        // 在新角色表中添加数据
        if (user.getRole() == 2) { // 变更为教师
            Teacher newTeacher = new Teacher();
            newTeacher.setUserId(user.getId());
            newTeacher.setName(user.getUsername()); // 默认使用用户名
            newTeacher.setIsDeleted(0);
            newTeacher.setCreateTime(LocalDateTime.now());
            newTeacher.setUpdateTime(LocalDateTime.now());
            teacherMapper.insert(newTeacher);
        } else if (user.getRole() == 1) { // 变更为学生
            Student newStudent = new Student();
            newStudent.setUserId(user.getId());
            newStudent.setName(user.getUsername()); // 默认使用用户名
            newStudent.setIsDeleted(0);
            newStudent.setCreateTime(LocalDateTime.now());
            newStudent.setUpdateTime(LocalDateTime.now());
            studentMapper.insert(newStudent);
        } else if (user.getRole() == 0) { // 变更为管理员
            Administrator newAdmin = new Administrator();
            newAdmin.setUserId(user.getId());
            newAdmin.setName(user.getUsername()); // 默认使用用户名
            newAdmin.setIsDeleted(0);
            newAdmin.setCreateTime(LocalDateTime.now());
            newAdmin.setUpdateTime(LocalDateTime.now());
            administratorMapper.insert(newAdmin);
        }

        // 更新用户表
        return this.updateById(user);
    }
} 